#include "cjson/cJSON.h"
#include "log/log.h"
#include "stdlib.h"

int main(int argc, char *argv[])
{
    char* name = "wy";
    int age = 18;

    cJSON *root = cJSON_CreateObject();

    cJSON_AddStringToObject(root, "name", name);
    cJSON_AddNumberToObject(root, "age", age);

    char* json = cJSON_PrintUnformatted(root);

    log_debug("生成json: %s", json);

    cJSON * root_parse = cJSON_Parse(json);

    cJSON *name_parse = cJSON_GetObjectItem(root_parse, "name");
    cJSON *age_parse = cJSON_GetObjectItem(root_parse, "age");

    if( name_parse == NULL || age_parse == NULL)
    {
        log_error("解析json失败");
        return -1;
    }

    char* name2 = name_parse->valuestring;
    int age2 = age_parse->valueint;

    log_debug("解析json: name: %s, age: %d", name2, age2);

    cJSON_Delete(root);
    cJSON_Delete(root_parse);
    free(json);

    return 0;
}